package tianrun.ziguan.api.calculate.config.service.influxdb.common;

import com.google.common.base.Joiner;
import io.swagger.annotations.ApiModel;
import org.apache.commons.lang3.StringUtils;
import org.influxdb.querybuilder.clauses.RawTextClause;
import tianrun.ziguan.api.calculate.config.enums.QueryTypeEnum;
import tianrun.ziguan.api.common.util.DateUtil;

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Set;
import java.util.regex.Pattern;

@ApiModel("inquirer")
public class InquirerBody {

    private final static Pattern p = Pattern.compile("^[1-5]?[0-9]m$|^60m$");
    private final static String i = "$|^";
    private final static String regexpLabel = "label =~ /^%s$/";

    private Collection<String> tableName;
    private Collection<String> label;
    private Long startTime;
    private Long endTime;
    @NotNull
    private QueryTypeEnum scope;
    private String groupTime;

    @AssertTrue(message = "参数有误")
    public boolean isValid() {
        if (tableName == null || tableName.stream().noneMatch(StringUtils::isNotBlank)) {
            return false;
        }
        if (label == null || label.stream().noneMatch(StringUtils::isNotBlank)) {
            return false;
        }
        if (groupTime != null && !p.matcher(groupTime).matches()) {
            return false;
        }
        if (endTime == null || startTime == null || endTime <= startTime) {
            return false;
        }
        LocalDateTime time = DateUtil.nowAsiaShanghai();
        Long currentTime = DateUtil.parseLong(time);
        Long aYearAgo = DateUtil.parseLong(time.minusDays(7));
        return startTime >= aYearAgo && endTime <= currentTime;
    }

    public Collection<String> getTableName() {
        return tableName;
    }

    public InquirerBody setTableName(Set<String> tableName) {
        this.tableName = tableName;
        return this;
    }

    public InquirerBody() {
    }

    public InquirerBody(Collection<String> tableName, Set<String> label, Long startTime, Long endTime, QueryTypeEnum scope, String groupTime) {
        this.tableName = tableName;
        this.label = label;
        this.startTime = startTime;
        this.endTime = endTime;
        this.scope = scope;
        this.groupTime = groupTime;
    }

    public Collection<String> getLabel() {
        return label;
    }

    public InquirerBody setLabel(Set<String> label) {
        this.label = label;
        return this;
    }

    public Long getStartTime() {
        return startTime;
    }

    public InquirerBody setStartTime(Long startTime) {
        this.startTime = startTime;
        return this;
    }

    public Long getEndTime() {
        return endTime;
    }

    public InquirerBody setEndTime(Long endTime) {
        this.endTime = endTime;
        return this;
    }

    public QueryTypeEnum getScope() {
        return scope;
    }

    public InquirerBody setScope(QueryTypeEnum scope) {
        this.scope = scope;
        return this;
    }

    public String getGroupTime() {
        return groupTime;
    }

    public InquirerBody setGroupTime(String groupTime) {
        this.groupTime = groupTime;
        return this;
    }

    public RawTextClause labelsCondition() {
        return new RawTextClause(String.format(regexpLabel, Joiner.on(i).join(label)));
    }

    public RawTextClause groupingBy() {
        if (groupTime == null) {
            return new RawTextClause("label");
        }
        return new RawTextClause(String.format("label, time(%s)", groupTime));
    }
}
